import Configure from '../activity-configure/configure.vue'
import Pages from '../../pages/pages.vue'
import { showMsg, getActivityList, updateActivity } from '../../../vuex/action'

export default {
  name: 'ActivityList',
  components: {
    'activity-configure': Configure,
    'vue-pages': Pages
  },
  vuex: {
    getters: {
      fresh: state => state.acListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetActivityList: getActivityList,
      xUpdateAc: updateActivity
    }
  },
  data() {
    return {
      showConfigure: false,
      activityList: [],
      ac: {
        id: 1,
        name: '',
        gold: 0,
        title: '',
        skipUrl: 1,
        startTime: '',
        endTime: '',
        invalid: 0,
        hide: 0
      },
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  ready() {
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetActivityList({
        start: start,
        limit: limit
      })
        .then((data) => {
          this.activityList = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    modifyAc(list) {
      this.showConfigure = true
      this.ac = list
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
